package com.yjf.ljr.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.AccountOverviewDataClass;
import com.yjf.ljr.dataclass.AccountOverviewDataClass.AccountInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.util.SPreferences;
import com.yjf.ljr.util.AppUtil.TextType;

public class AcountInformationActivity extends IjActivity implements
		OnClickListener {
	private TextView mTvUserName; // 用户名

	private TextView mTvAvailableBalance;// 可用余额
	private TextView mTvBlockedBalances; // 冻结金额
	private TextView mTvTotalMoney; // 资产总额
	private TextView mTvRegisterTime; // 注册时间
	private TextView mTvCardType;// 证件类型
	private TextView mTvEmailbonding;// 邮箱认证
	private TextView mTvPhonebonding; // 手机号认证

	private TextView mTvRealName; // 真实姓名
	private TextView mTvIdCard; // 身份证号
	private TextView mTvAuthState; // 身份证号认证状态
	private TextView mTvPhone; // 电话号码
	private TextView mTvEmail; // 电子邮件
	private TextView mTvPayAccount; // 支付账户
	private TextView mTvDredgeState; // 开通状态
	private TextView mTvUpdatePhoneNumber;// 修改手机号
	private TextView mTvUpdatePwd;// 修改密码
	@ID("tvpwdstate")
	private TextView mTvPwdState;// 登陆密码状态
	static AcountInformationActivity instance;

	@ID("fail_acountinformation")
	private LinearLayout mLinearLayout;
	@ID("llacountinformation")
	private LinearLayout mLAcountinformation;
	@ID("ivstate")
	private ImageView mIvState;
	@ID("ivstates")
	private ImageView mIvStates;
	@ID("tvupdateemail")
	private TextView mTvUpdateEmail;
	private boolean isOnLine = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_accountinformation);
		initTextView();
		showProgressDialog();
		new AccountOverviewTask().execute();

	}

	private void initTextView() {
		setLeftBtnClick();
		setTitleStr("账户信息");
		mTvRealName = (TextView) findViewById(R.id.tvrealname);// 真实姓名
		mTvIdCard = (TextView) findViewById(R.id.tvidcardnumber);// 身份证号
		mTvAuthState = (TextView) findViewById(R.id.tvcertificatestatus);// 身份证号认证状态
		mTvPhone = (TextView) findViewById(R.id.tvphonenumber);// 电话号码
		mTvEmail = (TextView) findViewById(R.id.tvemailcontent);// 电子邮件
		mTvPayAccount = (TextView) findViewById(R.id.tvpayaccountname);// 支付账户
		mTvDredgeState = (TextView) findViewById(R.id.tvopeningstatus);// 开通状态

		mTvUpdatePhoneNumber = (TextView) findViewById(R.id.tvupdatephonenumber);// 修改手机号
		mTvUpdatePwd = (TextView) findViewById(R.id.tvupdatepwd);// 修改密码
		mTvUpdatePhoneNumber.setOnClickListener(this);
		mTvUpdatePwd.setOnClickListener(this);
		mTvUpdateEmail.setOnClickListener(this);
	}

	private void refresh() {

		new AccountOverviewTask().execute();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {

		if (CommonData.FLAG_LOGIN_SERVER) {
			refresh(); // refresh account
			CommonData.FLAG_LOGIN_SERVER = false;
		} else {
			if (requestCode == CommonData.REQUEST_CODE_LOGIN && !isOnLine) {

				mLinearLayout.setVisibility(View.VISIBLE);
				mLAcountinformation.setVisibility(View.GONE);
				mLinearLayout
						.setOnClickListener(AcountInformationActivity.this);
				return;

			} else {
				mLinearLayout.setVisibility(View.GONE);
				return;

			}
		}
		super.onActivityResult(requestCode, resultCode, intent);

	}

	class AccountOverviewTask extends AsyncTask<Void, Void, String> {

		private String mFailstr;
		private AccountOverviewDataClass data = new AccountOverviewDataClass();

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
		}

		@Override
		protected String doInBackground(Void... params) {
			/*
			 * RequestObject requestObject = new RequestObject();
			 * requestObject.method = "accountOverview"; String requestUrl =
			 * RequestBuilder.build(requestObject); try {
			 * IjConnect.doRequest(requestUrl, data); if (data.code == null) {
			 * mFailstr = getResources().getString(R.string.loaddata_exception);
			 * } else if (!data.code.equals(CommonData.RESULT_SUCCESS)) {
			 * mFailstr = data.msg; } } catch (Exception e) { // TODO: handle
			 * exception mFailstr =
			 * getResources().getString(R.string.loaddata_exception);
			 * e.printStackTrace(); }
			 * 
			 * return null;
			 */

			RequestObject reqObj = new RequestObject();
			reqObj.method = "appAccountInfo";
			return requestDataAndGetErrorMsg(reqObj, data);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			if ("-1".equals(data.code)) {
				loginWebview();
				isOnLine = false;
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
				mLinearLayout.setVisibility(View.VISIBLE);
				mLAcountinformation.setVisibility(View.GONE);
				mLinearLayout
						.setOnClickListener(AcountInformationActivity.this);
				dismissProgressDialog();
			} else {
				isOnLine = true;
				mLAcountinformation.setVisibility(View.VISIBLE);
				mLinearLayout.setVisibility(View.GONE);
				AccountInfo date = (AccountInfo) data.accountInfo;
				if (date != null) {
					AppUtil.setViewText(mTvRealName, date.realName, true,
							TextType.TYPE_TEXT);

					AppUtil.setViewText(mTvIdCard, date.idCard, true,
							TextType.TYPE_TEXT);
					if (!TextUtils.isEmpty(date.authState)) {

						mTvAuthState.setText(date.authState);
						mIvStates.setVisibility(View.VISIBLE);

					} else {
						mTvAuthState.setText("");
						mIvStates.setVisibility(View.GONE);
					}

					String temp_phone_prv = date.phone.substring(0, 2);
					String temp_phone_net = date.phone.substring(9, 11);

					AppUtil.setViewText(mTvPhone, temp_phone_prv + "*******"
							+ temp_phone_net, true, TextType.TYPE_TEXT);
					AppUtil.setViewText(mTvEmail, date.email, true,
							TextType.TYPE_TEXT);

					AppUtil.setViewText(mTvPayAccount, date.payAccount, true,
							TextType.TYPE_TEXT);
					AppUtil.setViewText(mTvPwdState, date.pswState, true,
							TextType.TYPE_TEXT);

					if (!TextUtils.isEmpty(date.dredgeState)) {

						mTvDredgeState.setText(date.dredgeState);
						mIvState.setVisibility(View.VISIBLE);

					}
				} else {
					mTvDredgeState.setText("");
					mIvState.setVisibility(View.GONE);
				}

			}
			dismissProgressDialog();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		new AccountOverviewTask().execute();
		showProgressDialog();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.tvupdatephonenumber:
			Intent intent = new Intent();
			intent.setClass(AcountInformationActivity.this,
					UpdatePhoneActivity.class);
			intent.putExtra("user_phone", mTvPhone.getText().toString());
			startActivity(intent);
			break;
		case R.id.tvupdatepwd:
			startActivity(new Intent(AcountInformationActivity.this,
					UpdatePwdActivity.class));
			AcountInformationActivity.this.finish();
			break;
		case R.id.fail_acountinformation:
			new AccountOverviewTask().execute();
			showProgressDialog();
			break;
		case R.id.tvupdateemail:
			startActivity(new Intent(AcountInformationActivity.this,
					UpdateEmailActivity.class));
			break;
		default:
			break;
		}
	}
}
